/**
 * Created by Chen on 2016/4/10.
 */
define(function(require, exports, module){

    var $ = require('jquery');
    require('jquery.validate');
    require('jquery.icheck');
    require('bootstrap');
    require('bootstrap.select');
    require('jquery.layer');
    require('dateSelect');
    require('jquery.form');
    var List = require('../../util/list.js');

    // 初始化
    $('input').iCheck({
        checkboxClass: 'icheckbox_square-blue',
        radioClass: 'iradio_square-blue',
        increaseArea: '20%' // optional
    });

    //$("#paper-submit").click(function(){
    //    layer.msg('保存成功', {
    //        icon: 1,
    //        time: 4000 //4s后自动关闭
    //    });
    //    window.location.href = $CONFIG.base_url + "/student/paper/upload";
    //
    //});
    //var des = $("#description").val();
    //var up_file = $("#file").val();
    //var posting = $.post($CONFIG.base_url + "/commons/attachment/file_upload", {uid: $CONFIG.uid, description: des, file: up_file});
    //posting.done(function(data) {
    //   if(data == true) {
    //       layer.msg('保存成功', {
    //           icon:1,
    //           time: 4000 //4s后自动关闭
    //       });
    //       window.location.href = $CONFIG.base_url + "/student/paper/upload";
    //   }
    //});
    $("#paper-submit").click(function(){
        var form = $("#student-paper");
        var options = {
            url: $CONFIG.base_url +'/commons/attachment/file_upload',
            type: 'post',
            success: function (data) {
                if(data == "true") {
                    layer.msg('保存成功', {
                        icon:1,
                        time: 500 //4s后自动关闭
                    }, function () {
                        window.location.href = $CONFIG.base_url + "/student/paper/upload";
                    });
                }
            }
        };
        form.ajaxSubmit(options);
        return false;
    });

    exports.loadList = function(param){
        var list = new List();
        list.options = {
            template : "list-template",
            container : "list-data",
            url : $CONFIG.base_url + "/api/paper/list",
            param : param
        };
        list.load();
    };
    exports.loadList({'relation.id': '5'});

    $("#list-data").on("click",".paper-del",function(){
        var data = $.parseJSON($(this).attr("data"));
        var $this = $(this);
        layer.confirm('要删除该记录？', {
            btn: ['确认','取消'], //按钮
            shade: false //不显示遮罩
        }, function(){
            var posting = $.post($CONFIG.base_url + "/api/paper/del", data);
            posting.done(function(data){
                layer.msg('删除成功', {icon:1,time:1000});
                $this.parents("p").remove();
            }).fail(function() {
                layer.msg('无权限或出现错误，请联系管理员', {icon:9});
            })
        }, function(){
        });

    });

});
